package com.bangzhi.controller;


import com.bangzhi.form.*;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "数据入参")
public class TestController {


    @ApiOperation(value = "接受客户数据")
    @PostMapping("receiveCusData")
    public void receiveCusData( @RequestBody  CusCustomerForm cusCustomerForm){
    }



    public static void main(String[] args) throws Exception {
        File file = new File("/Users/kevinzeng/IdeaProjects/swagger_demo/src/main/java/com/bangzhi/form");
        TestController testController = new TestController();
        for (File listFile : file.listFiles()) {
            String absolutePath = listFile.getAbsolutePath();
            System.out.println(absolutePath);
            testController.addNotesAndExample(absolutePath);
        }
    }

    public void addNotesAndExample(String filePath) throws IOException {
        FileInputStream fis  = new FileInputStream(filePath);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        File file2 = new File(filePath);
        if (file2.exists()) {
            //
            file2.delete();
        }

        String tmp = null;
        StringBuilder sb = new StringBuilder();

        while ((tmp = br.readLine()) != null) {
            String content = "";
            int i = tmp.indexOf("@ApiModelProperty");
            if (i > 0 && !tmp.trim().startsWith("//")){
                int tmpLast括号 = tmp.lastIndexOf(")");
                int value = tmp.lastIndexOf("value");
                if (value>0){
                    // 有value的
                    // 直接加
                    int first_括号 = tmp.indexOf("(");
                    String text = tmp.substring(first_括号+1, tmpLast括号);
                    int textLast括号 = text.lastIndexOf("\"");
                    String remark = text.substring(text.indexOf("\"")+1,textLast括号);
                    if (remark.contains("required")){

                    }else {
                        sb.append("\n").append(tmp.substring(0,tmpLast括号)).append(" ").append(",example = \"").append(remark).append("\")");
                    }

                }else {
                    // 没有value的 加value
//                    @ApiModelProperty(value=""企业用户信息,example=""企业用户信息")    @ApiModelProperty("企业用户信息")
                    String remark = tmp.substring(tmp.indexOf("(\"") + 2, tmp.lastIndexOf("\")"));
                    sb.append(tmp.substring(0,tmp.indexOf("(")))
                            .append("(").append("value=\"").append(remark).append("\",")
                            .append("example=\"").append(remark).append("\")");
                }
            }else {
                sb.append(tmp);
            }
            sb.append("\n");
        }
        System.out.println(sb);

        FileOutputStream fos = new FileOutputStream(file2);
        fos.write(sb.toString().getBytes(StandardCharsets.UTF_8));
        fos.flush();
        fos.close();
    }


    public  void a(String filePath) throws Exception {
        FileInputStream fis  = new FileInputStream(filePath);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        File file2 = new File(filePath);
        if (file2.exists()) {
            //
            file2.delete();
        }

        String tmp = null;
        StringBuilder sb = new StringBuilder();

        while ((tmp = br.readLine()) != null) {
            int i = tmp.indexOf("//");
            if (i > 1){
                String first = tmp.substring(0, i);
                sb.append("\n").append("    @ApiModelProperty(\""+tmp.substring(i+2)+"\")").append("\n").append(tmp);
            }else {
                sb.append(tmp);
            }
            sb.append("\n");
        }

        FileOutputStream fos = new FileOutputStream(file2);
        fos.write(sb.toString().getBytes(StandardCharsets.UTF_8));
        fos.flush();
        fos.close();
    }

}
